<?php
namespace Common\Library;

/**
 * Description of leduwo.com
 *
 * @author gangguox
 */
class leduwo{

    protected $BookArticle;
    protected $detailUrl;
    protected $id;

    public function __construct($id,$url = ''){
        header("Content-Type:text/html;charset=utf-8;");
        $this->id = $id;
        if(empty($url)){
            $this->update();exit;
        }
        $this->detailUrl = $url;
        $this->addNewBook();

    }


    //添加一本新书
    protected function addNewBook(){
        set_time_limit(0);
        $pathFile = './Runtime/Collect/leduwo/addbook.html';
        \Common\Library\COM\CollectDo::downPage($this->detailUrl,$pathFile);
        $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile);
        $bookInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'<DIV id=infoBox1>','<br><br><script language="javascript" defer="defer">');
        unset($getStr,$pathFile);
        //提取书籍名称
        $bookName = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'.html" target=_blank>','</A></H1>');
        //提取作者
        $author = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'<ADDRESS>作者：','</ADDRESS></DIV>');
        //p($author);p($bookName);exit;
        if(empty($bookName) || empty($author)){
            $this->writeLog('书名,作者,或为空!:'.$this->detailUrl,'ERR');
            exit('书名,作者,或为空!:'.$this->detailUrl);
        }

        //作者处理
        $author_id = D('Common/BookAuthor')->getAuthorId($author);
        if(empty($author_id)){
            $this->writeLog('作者ID错误:'.$author_id.' @@ '.$author.' @@ '.$this->detailUrl,'ERR');
            exit();
        }
        $Book = new \Common\Model\BookModel();
        $unique = md5($bookName.$author_id);
        //书本重复检测
        $book_id = $Book->checkBookRepeart($unique);
        //p($book_id);exit;
        if(empty($book_id)){//新书入库
            //提取简介
            $intro = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'<DIV id="contentA">','<br><br><script language="javascript" defer');
            $intro = strip_tags($intro);
            $intro = trim($intro);
            //提取分类
            $cate = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'类别：','<td>收 藏 数');
            $cate = strip_tags($cate);
            $cate = \Common\Library\COM\CollectDo::changeCateName($cate);
            if(empty($cate)){
                $this->writeLog('入库错误,没有分类:'.var_export($cate,1).'@@'.var_export(D('Common/BookCate')->getLastSql(),1),'ERR');
                exit('入库错误,没有获取到分类');
            }
            //提取封面图片
            $image = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'src="http://','" width=200');
            $image = 'http://'.$image;
            //分类处理
            $cate_id = D('Common/BookCate')->getCateId($cate);
            //书籍状态
            $bookStatus = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'字...<BR>','... </P>');
            if($bookStatus == '已完成'){
                $status = 'end';
            }else{
                $status = 'ing';
            }
            //封面图片处理
            $imagePath = \Common\Library\COM\CollectDo::doImagePath();

            $dataBook = array(
                'book_name'=>$bookName,
                'author_id'=>$author_id,
                'status'=>$status,
                'cate_id'=>$cate_id,
                'introduction'=>  htmlentities($intro,ENT_COMPAT,'UTF-8'),
                'img'=>$imagePath,
                'create_time'=>  time(),
                'year'=> date('Y'),
                'month'=>  date('m'),
                'day'=>  date('d'),
                'unique'=>$unique
            );
            //p($dataBook);exit;
            $book_id = $Book->saveBook($dataBook);
            \Common\Library\COM\CollectDo::downPage($image, '.'.$imagePath);
        }
        //新书开始入库
        if(empty($book_id)){
            $this->writeLog('入库错误,书本未入库:'.  var_export($Book->getLastSql(),1),'ERR');
            exit('入库错误,书本未入库');
        }else{
            $this->writeLog('bookinstall.新书开始入库:'.var_export($bookName,1));
        }
        $listInfoUrl = \Common\Library\COM\CollectDo::getSubstr($bookInfo,'<H1><A href="','" target=_blank>');
        $this->articleCollection($listInfoUrl,$book_id);
        unset($bookInfo,$Book);
    }

    //章节列表名称入库
    protected function articleCollection($listInfoUrl,$book_id){
        $pathFile = './Runtime/Collect/leduwo/listInfo.html';
        \Common\Library\COM\CollectDo::downPage($listInfoUrl,$pathFile);
        $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile);
        $listInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'cellpadding="3" cellspacing="1" class="acss">','</table>');
        unset($pathFile,$getStr);
        //p($listInfo);
        //章节名称和篇名入库
        $count = mb_substr_count($listInfo,'<td ');
        $offset = 0;
        $this->BookArticle = new \Common\Model\BookArticleModel();
        for($i=0;$i<$count;$i++){
            $this->writeLog($val.':章节入库开始!');
            $nowInfo = \Common\Library\COM\CollectDo::getSubstr($listInfo,'','</td>');
            $piece = mb_strpos($nowInfo, 'class="vcss">');
            if($piece > 0){
                $piece_name =  \Common\Library\COM\CollectDo::getSubstr($nowInfo,'class="vcss">','</td>');
                $piece_num =  0;//\Common\Library\COM\CollectDo::getPieceNum($piece_name);
                $listInfo =  \Common\Library\COM\CollectDo::getSubstr($listInfo,'</td>');
                continue;
            }
            //获取章节名称
            $article_name = \Common\Library\COM\CollectDo::getSubstr($nowInfo,'" title="');//trim($article_name);
            $article_name = \Common\Library\COM\CollectDo::getSubstr($article_name,'','">');
            if(empty($article_name)){
                $this->writeLog('章节名称不能为空!@Value:'.var_export($listInfo,1).'@URL:'.$this->detailUrl,'ERR');
                exit;
            }
            $unique = md5($book_id.$article_name);
            $result = $this->BookArticle->checkArtcileRepeat($unique);
            if($result){//重复章节检测
                //$this->writeLog($article_name.' ====> 重复章节');
                $listInfo =  \Common\Library\COM\CollectDo::getSubstr($listInfo,'</td>');
                continue;
            }else{//采集新章节
                $article_num = \Common\Library\COM\CollectDo::getArticleNum($article_name);
                //获取章节内容url地址
                $article_url = \Common\Library\COM\CollectDo::getSubstr($nowInfo,'<a href="','" title="');
                $article_url = rtrim($listInfoUrl,'index.html').$article_url;
                //获取章节内容并存储
                $contentPath = $this->contentCollection($article_url);
                $articleDate = array(
                    'book_id'=> $book_id,
                    'article_name'=> $article_name,
                    'create_time'=>  time(),
                    'year'=>  date('Y'),
                    'month'=>  date('m'),
                    'day'=>  date('d'),
                    'article_num'=> $article_num,
                    'update_time' => time(),
                    'unique'=>$unique,
                    'piece_num'=> $piece_num,
                    'piece_name'=> $piece_name,
                    'content'=>$contentPath
                );
               // p($article_url);exit;
                $article_id = $this->BookArticle->add($articleDate);
                if(empty($article_id)){
                    $this->writeLog('章节存储失败!@'.var_export($articleDate,1).var_export($this->BookArticle->getDbError(),1),'ERR');
                    exit();
                }
            }
            $listInfo =  \Common\Library\COM\CollectDo::getSubstr($listInfo,'</td>');
        }
        unset($this->BookArticle);
        $this->writeLog($this->detailUrl.'  本书入库完成!');
    }

    //章节内容入库
    protected function contentCollection($contentUrl = ''){
        if(empty($contentUrl)){
            $this->writeLog('内容章节URL地址空:'.var_export($contentUrl,1),'ERR');
            exit;
        }
        $savePath = './Runtime/Collect/leduwo/content.html';
        \Common\Library\COM\CollectDo::downPage($contentUrl, $savePath);
        $contentA = \Common\Library\COM\CollectDo::getHtml($savePath);
        if(empty($contentA)){
            \Common\Library\COM\CollectDo::downPage($contentUrl, $savePath);
            //sleep(3);
            $contentA = \Common\Library\COM\CollectDo::getHtml($savePath);
        }
        $contentA = \Common\Library\COM\CollectDo::getSubstr($contentA,'<div id="content">','<div id="adbottom">');

        $replace = array(
                '</div>','<a href="','<a href=','<a','</a>',
                'www.83zw.com','http://www.83zw.com',
                'www.leduwo.com','leduwo.com','http://www.leduwo.com',
                '83zw.com','83中文网');
        $contentT = str_replace($replace,'', $contentA);unset($contentA);

        if(!\Common\Library\COM\CollectDo::checkSpecialStr($contentT)){
            $data = array('id'=>$this->id,'dotime'=>time(),'disabled'=>4,'remark'=>'采集章节内容有误,包含特殊字符:'.$contentUrl);
            \HelloAdmin\Model\CrontabModel::updateData($data);
            $this->writeLog('采集章节内容有误,包含特殊字符:'.var_export($contentUrl,1).'@@'.var_export($contentT,1),'ERR');
            exit();
        }
        $contentT = htmlentities($contentT,ENT_COMPAT,'UTF-8');
        $contentPath = \Common\Library\COM\CollectDo::doContentPath();
        $handel = fopen('.'.$contentPath,'x+');
        if($handel && fwrite($handel,$contentT) !== FALSE){
            fclose($handel);
            return $contentPath;
        }
        $this->writeLog('章节内容存储失败!@'.var_export($contentPath,1).'|@|'.var_export($contentUrl,1),'ERR');
        $this->writeLog('章节内容'.var_export($contentT,1),'ERR');
        exit();
    }

    //采集日志记录
    private function writeLog($message,$level='INFO'){
        \Think\Log::write($message,$level,$type='','./Runtime/Collect/leduwo/logs-'.  date('Ymd').'.log');
    }

    public function update(){
        $url = 'http://www.leduwo.com/ldw_lastupdate/1.htm';
        set_time_limit(0);
        $pathFile = './Runtime/Collect/leduwo/updatelist.html';
        //\Common\Library\COM\CollectDo::downPage($url, $pathFile);
        $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile);
        $updatelist = \Common\Library\COM\CollectDo::getSubstr($getStr,'更新</td>','<div class="pages">');
        unset($getStr,$pathFile);
        //p($updatelist);//exit;
        //获取书籍名称
        $updatelist = ltrim($updatelist,'</tr>');
        $count = mb_substr_count($updatelist,'<tr>');
        $Book = new \Common\Model\BookModel();
        $Author = new \Common\Model\BookAuthorModel();
        for($i=0;$i<$count;$i++){
            $nowInfo = \Common\Library\COM\CollectDo::getSubstr($updatelist,'','</tr>');
            $bookName = \Common\Library\COM\CollectDo::getSubstr($nowInfo,'.htm">','</a></td>');
            $authorName = \Common\Library\COM\CollectDo::getSubstr($nowInfo,'target="_blank"> ','align="center">');
            $articleName = \Common\Library\COM\CollectDo::getSubstr($authorName,'','</a></td>');
            $authorName = \Common\Library\COM\CollectDo::getSubstr($authorName,'<td class="odd">');
            $authorName = trim(strip_tags($authorName));
            if(empty($bookName) || empty($authorName)){
                $this->writeLog('书名,作者,或为空!:'.$this->detailUrl,'ERR');
                exit('书名,作者,或为空!:'.var_export($value,1));
            }
            //p($this->detailUrl);p($authorName);exit;;
            $author_id = $Author->getAuthorId($authorName);
            $unique = md5($bookName.$author_id);
            //书本重复检测
            $book_id = 1;//$Book->checkBookRepeart($unique);
            if($book_id > 0){//更新已有的书籍
                if(empty($articleName)){
                    $this->writeLog($offset.':====> 章节名称空'.var_export($updatelist,1),'ERR');
                    exit();
                }
                $unique = md5($book_id.$articleName);
                $this->BookArticle = new \Common\Model\BookArticleModel();
                $result = $this->BookArticle->checkArtcileRepeat($unique);

                if($result){//重复章节检测
                    $this->writeLog($articleName.' ====> 重复章节');
                    //continue;
                }
                //获取章节数
                $num = \Common\Library\COM\CollectDo::getArticleNum($articleName);
                $piece_num = \Common\Library\COM\CollectDo::getPieceNum($articleName);
                //获取章节内容地址
                $articleUrl = \Common\Library\COM\CollectDo::getSubstr($updatelist,'s3"><a href="','target="_blank">'.$articleName);
                $articleUrl = rtrim($articleUrl,'"');
                $articleUrl = 'http://www.biquge.tw'.$articleUrl;
                p($articleUrl);exit;
                //获取章节内容并存储
                $contentPath = $this->contentCollection($articleUrl);
                $new = $this->BookArticle->getNewArticle($book_id);

                if(empty($num) || empty($new['article_num']) || $new['article_num']==($num-1)){//默认更新一个章节
                    $articleDate = array(
                        'book_id'=> $book_id,
                        'article_name'=> $articleName,
                        'create_time'=>  time(),
                        'year'=>  date('Y'),
                        'month'=>  date('m'),
                        'day'=>  date('d'),
                        'article_num'=> $num,
                        'update_time' => time(),
                        'unique'=>$unique,
                        'piece_num'=> $piece_num,
                        'piece_name'=> '',
                        'content'=>$contentPath
                    );
                    //p($articleDate);//exit;
                    $article_id = $this->BookArticle->add($articleDate);
                    //$this->writeLog('====> 更新了 一个 章节 ======>');
                    continue;
                }else{//更新多个章节
                    $this->detailUrl = \Common\Library\COM\CollectDo::getSubstr($updatelist,'class="s2"><a href="',' target="_blank">'.$bookName);
                    $this->detailUrl = rtrim($this->detailUrl,'"');
                    $pathFile = './Runtime/Collect/leduwo/updateaddbook.html';
                    \Common\Library\COM\CollectDo::downPage($this->detailUrl, $pathFile);
                    $getStr = \Common\Library\COM\CollectDo::getHtml($pathFile,'','utf-8');
                    $listInfo = \Common\Library\COM\CollectDo::getSubstr($getStr,'<div id="list">','<div id="footer" name="footer">');
                    //p($listInfo);

                    //章节名称和篇名入库
                    $this->articleCollection($listInfo,$book_id);
                    $this->writeLog('====>  对比更新 多个 章节内容 =====>');
                }
            }
            $offset = mb_strpos($updatelist, 's5');
            $updatelist = \Common\Library\COM\CollectDo::getSubstr($getStr,'</tr>');
            $this->writeLog('====> 新书跳过，更新下一个章节 ====>');
            continue;
        }
    }

}
